package gui;

import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.SoftBevelBorder;

import util.MyFactory;
//TODO day01 设置登录窗体
@SuppressWarnings("serial")
public class LoginFrame extends BaseFrame implements KeyListener{
	//设置主面板
	private JPanel pane;
	private JTextField text;
	private JPasswordField pwd;
	//private ClientContext context;
	//DOTO day01 取得整个屏幕的宽和高
	private final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
	private final int WEIGHT = Toolkit.getDefaultToolkit().getScreenSize().width;
	public LoginFrame() {
		init();
	}
	//DOTO day02 设置登录界面的注入式构造器
	public LoginFrame(ClientContext clientContext){
		this();
		this.context=clientContext;
	}
	@Override
	protected void init() {
		this.setSize(300,200 );
		this.setLocation((WEIGHT - 300) / 2, (HEIGHT - 200) / 2);
		this.setTitle("学生在线考试系统-请登录");
		this.add(createPanel());
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				context.exit();
			}
		});
		this.addKeyListener(this);
	}
	/**创建主面板*/
	private JPanel createPanel() {
		pane=new JPanel();
		pane.setSize(300,200 );
		pane.setOpaque(false);
		pane.setLayout(null);
		
		createLogo();
		createName();
		createPwd();
		createButtons();
		
		return pane;
	}
	/**创建登录标签*/
	private void createLogo() {
		JLabel jl=new JLabel("登录");
		jl.setBounds(134, 10, 100, 20);
		pane.add(jl);
	}
	/**创建登录和退出按钮*/
	private void createButtons() {
		JButton ok=new JButton("登录");
		ok.setBounds(75, 120, 60, 30);
		//TODO day02 01 给登录按钮增加事件监听
		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				context.login();
			}
		});
		ok.addKeyListener(this);
		pane.add(ok);
		JButton cancle=new JButton("退出");
		cancle.setBounds(170, 120, 60, 30);
		cancle.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				context.exit();
			}
		});
		cancle.addKeyListener(this);
		pane.add(cancle);
	}
	private void createPwd() {
		JLabel jl=new JLabel("密码:");
		jl.setBounds(44, 80, 100, 20);
		pane.add(jl);
		pwd=new JPasswordField(30);
		pwd.setBorder(new SoftBevelBorder(1));
		pwd.setBounds(90, 80, 150, 20);
		pane.add(pwd);
	}
	private void createName() {
		JLabel jl=new JLabel("编号:");
		jl.setBounds(44, 50, 100, 20);
		pane.add(jl);
		text=new JTextField(30);
		text.setBounds(90, 50, 150, 20);
		text.setBorder(new SoftBevelBorder(1));
		pane.add(text);
	}
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		ImageIcon icon=MyFactory.getImageIcon("login.png");
		JLabel jl1=new JLabel(icon);
		jl1.setBounds(0, 0, 300, 200);
		this.add(jl1);
	}
	public int getId(){
		int id=-1;
		try {
			id = Integer.parseInt(text.getText());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id;
	}
	public String getPwd(){
		return new String(pwd.getPassword());
	}
	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode()==KeyEvent.VK_ENTER) {
			context.login();
		}
		if (e.getKeyCode()==KeyEvent.VK_ESCAPE) {
			context.exit();
		}
	}
	@Override
	public void keyReleased(KeyEvent e) {

	}
	@Override
	public void keyTyped(KeyEvent e) {
		
	}
}
